package com.atlzk.renting.user.controller;

import cn.hutool.core.util.ObjectUtil;
import com.atlzk.common.entity.user.UserEntity;
import com.atlzk.common.utils.FileBlobUtil;
import com.atlzk.common.utils.PageUtils;
import com.atlzk.common.utils.R;
import com.atlzk.renting.user.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;



/**
 * 
 *
 * @author liuzekun
 * @email 2803587841@gmail.com
 * @date 2021-07-16 20:01:15
 */
@RestController
@CrossOrigin
@RequestMapping("user/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("user:user:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("user:user:info")
    public R info(@PathVariable("id") Long id){
		UserEntity user = userService.getById(id);

        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("user:user:save")
    public R save(@RequestBody UserEntity user){
		userService.save(user);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("user:user:update")
    public R update(@RequestBody UserEntity user){
		userService.updateById(user);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("user:user:delete")
    public R delete(@RequestBody Long[] ids){
		userService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 登陆
     */
    @PostMapping("/login")
    public R login(@RequestBody UserEntity userEntity) {
        return userService.login(userEntity);
    }
    /**
     * 验证码登陆
     */
    @GetMapping("/loginCode/{telephoneOrEmail}/{code}")
    public R loginCode(@PathVariable("telephoneOrEmail") String telephoneOrEmail,@PathVariable("code") String code) {
        return userService.loginCode(telephoneOrEmail, code);
    }

    /**
     * 获取验证码
     */
    @GetMapping("/sendCode/{telephoneOrEmail}")
    public R sendCode(@PathVariable("telephoneOrEmail") String telephoneOrEmail) {
        return userService.sendCode(telephoneOrEmail);
    }

    /**
     * 注册
     * @param userEntity
     * @return
     */
    @PostMapping("/register")
    public R register(@RequestBody UserEntity userEntity) {

        return userService.register(userEntity);
    }

    /**
     * 验证用户
     * @param telephoneOrEmail
     * @return
     */
    @GetMapping("/verifyEmail/{telephoneOrEmail}")
    public R verifyEmail(@PathVariable("telephoneOrEmail") String telephoneOrEmail) {
        UserEntity user = userService.getOne(new QueryWrapper <UserEntity>().eq("email", telephoneOrEmail).or().eq("telephone", telephoneOrEmail));
        if (ObjectUtil.isNotNull(user)) {
            return R.ok("用户存在");
        }
        return R.error("用户不存在");
    }

    /**
     * 根据手机号 邮箱 用户名  查询用户信息
     */
    @GetMapping("/findOne/{userNum}")
    public R findOne(@PathVariable("userNum") String userNum) {
        return userService.findOne(userNum);
    }

    /**
     * 图片上传  oss
     * @param file
     * @return
     */
    @RequestMapping("/uploadFile")
    public String uploadFile(MultipartFile file) {
        String s = "";
        try {
            s = FileBlobUtil.uploadFile(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return s;
    }

}
